package com.glory.hzlionpda.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hzlionpda.R;
import com.glory.hzlionpda.adapter.ChooseWaferListAdapter;
import com.glory.hzlionpda.base.Constant;
import com.glory.hzlionpda.bean.DefectCodeBean;
import com.glory.hzlionpda.bean.LotBean;
import com.glory.hzlionpda.bean.WaferBean;
import com.glory.hzlionpda.bean.WaferLotBean;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class DefectInputActivity extends FreeUI_BaseActivity {


    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvDefectDesc)
    TextView tvDefectDesc;
    @BindView(R.id.ivArrow)
    ImageView ivArrow;
    @BindView(R.id.flDefectDesc)
    FrameLayout flDefectDesc;
    @BindView(R.id.tvType)
    TextView tvType;
    @BindView(R.id.tvDefectCode)
    TextView tvDefectCode;
    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.tvWafer)
    TextView tvWafer;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.etReason)
    FreeUI_ClearEditText etReason;

    private LotBean lotBean;
    private List<String> defectDescList = new ArrayList<>();
    private FreeUI_GeneralPop defectDescPop;
    private int selectPosition = -1;
    private List<DefectCodeBean> defectCodeBeanList;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_defect_input;
    }

    @Override
    protected void initView() {
        lotBean = (LotBean) getIntent().getSerializableExtra("lotBean");
        if (lotBean == null) {
            showToast("对象为空，请重新请求页面", false);
            DefectInputActivity.this.finish();
            return;
        }
        defectCodeBeanList = lotBean.getDEFECTCODELIST();
        for (DefectCodeBean defectCodeBean : lotBean.getDEFECTCODELIST()) {
            defectDescList.add(defectCodeBean.getDATA_1_VALUE());
            defectCodeBean.setRESPONSIBILITY(Constant.CURRENT_USER);
        }
    }


    @Override
    protected void initData() {
        etLotId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (selectPosition != -1) {
                    defectCodeBeanList.get(selectPosition).setLOTID(editable.toString());
                }
            }
        });

        etReason.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (selectPosition != -1) {
                    defectCodeBeanList.get(selectPosition).setREASONS(editable.toString());
                }
            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("异常填写");
        freeUI_titleView.setLeftListener(view -> saveLotBean());
        freeUI_titleView.setRightListener("提交", v -> {
            saveLotBean();
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void saveLotBean() {
        lotBean.setDEFECTCODELIST(defectCodeBeanList);
        setResult(2101, new Intent().putExtra("lotBean", lotBean));
        DefectInputActivity.this.finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 当按下返回键时所执行的命令
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            saveLotBean();
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @OnClick({R.id.flDefectDesc, R.id.llDefectWafer})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.flDefectDesc:
                if (defectDescPop != null) {
                    if (defectDescPop.isShowing()) {
                        defectDescPop.dismiss();
                    } else {
                        FreeApi_PopUtils.setArrowShow(ivArrow);
                        defectDescPop.showAsDropDown(flDefectDesc, 0, 0);
                    }
                } else {
                    if (defectDescList.size() == 0)
                        return;
                    defectDescPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flDefectDesc.getWidth(), defectDescList,
                            ivArrow, (s, i) -> {
                                selectPosition = i;
                                tvDefectDesc.setText(s);
                                tvType.setText(defectCodeBeanList.get(i).getTYPE() + "/" + defectCodeBeanList.get(i).getTYPENAME());
                                tvDefectCode.setText(defectCodeBeanList.get(i).getREASONCODE());
                                tvUser.setText(defectCodeBeanList.get(i).getRESPONSIBILITY());
                                tvWafer.setText(defectCodeBeanList.get(i).getUNITLIST());
                                etLotId.setText(defectCodeBeanList.get(i).getLOTID());
                                etReason.setText(defectCodeBeanList.get(i).getREASONS());
                            });
                    FreeApi_PopUtils.setArrowShow(ivArrow);
                    defectDescPop.showAsDropDown(flDefectDesc, 0, 0);
                }
                break;
            case R.id.llDefectWafer:
                if (selectPosition == -1) {
                    showToast("还未选择异常说明", false);
                    return;
                }
                final FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog = new FreeUI_GeneralFragmentDialog();
                View view = LayoutInflater.from(this).inflate(R.layout.dialog_choose_wafer, null);
                RecyclerView rvWaferList = view.findViewById(R.id.rvWaferList);
                CheckBox cbCheckAll = view.findViewById(R.id.cbCheckAll);
                rvWaferList.setLayoutManager(new LinearLayoutManager(this));
                rvWaferList.setItemAnimator(new DefaultItemAnimator());
                rvWaferList.addItemDecoration(new DividerItemDecoration(this, 1));
                List<WaferBean> waferBeanList = new ArrayList<>();
                for (WaferLotBean waferLotBean : lotBean.getWAFERLIST()) {
                    if (waferLotBean.getLOTID().equals(lotBean.getLOTID())) {
                        waferBeanList.addAll(waferLotBean.getWAFER());
                        break;
                    }
                }
                for (WaferBean waferBean : waferBeanList) {
                    waferBean.setSelect(false);
                }
                ChooseWaferListAdapter chooseWaferListAdapter = new ChooseWaferListAdapter(waferBeanList);
                chooseWaferListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
                chooseWaferListAdapter.isFirstOnly(true);
                rvWaferList.setAdapter(chooseWaferListAdapter);
                cbCheckAll.setOnCheckedChangeListener((compoundButton, isChecked) -> chooseWaferListAdapter.setAll(isChecked));
                TextView tvConfirm = view.findViewById(R.id.tvConfirm);
                TextView tvCancel = view.findViewById(R.id.tvCancel);
                tvConfirm.setOnClickListener(view12 -> {
                    int count = 0;
                    StringBuilder stringBuilder = new StringBuilder();
                    for (WaferBean waferBean : chooseWaferListAdapter.getData()) {
                        if (waferBean.isSelect()) {
                            count++;
                            stringBuilder.append(waferBean.getWAFERID());
                            stringBuilder.append(",");
                        }
                    }
                    String str = stringBuilder.toString();
                    if (count > 0) {
                        freeUI_generalFragmentDialog.dismiss();
                        defectCodeBeanList.get(selectPosition).setUNITLIST(str.substring(0, str.length() - 1));
                        defectCodeBeanList.get(selectPosition).setTRANSQTY1(String.valueOf(count));
                        tvWafer.setText( defectCodeBeanList.get(selectPosition).getUNITLIST());
                    } else {
                        showToast("未选择片号", false);
                    }
                });
                tvCancel.setOnClickListener(view1 -> freeUI_generalFragmentDialog.dismiss());
                freeUI_generalFragmentDialog.setLayout(view);
                freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "choose_wafer_dialog");
                break;
        }
    }
}
